﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Go.Tools;

namespace Go.Model
{
    public abstract class Player: IObservable
    {
        Color _color;
        String _name;
        List<Group> _groups;
        Player _opponent;
        Position _lastPlayed;
        bool _hasMadeAFirstMove;

       public Player(Color aColor, String name)
        {
            this._color = aColor;
            this._name = name;
           // _points = 0;
            _groups = new List<Group>();
            _lastPlayed = null;
            HasMadeAFirstMove = false;
        }

        public void putStone(Position aPosition)
        {
            doMove(aPosition);
            aPosition.posicionOcupada();
        }

        public void doMove(Position aPosition)
        {
            Group aGroup = new Group(aPosition, this);
            _hasMadeAFirstMove = true;
            _lastPlayed = aPosition;
        }

        public void simulatePutStone(Position aPosition)
        {
            doMove(aPosition);
        }

        public void addGroup(Group aGroup) {
            _groups.Add(aGroup);
        }
        
        public void removeStone(Position aPosition)
        {
            aPosition.Group.disassociateGroups(aPosition);
        }
               
        public bool isWhite()
        {
            return this._color == Color.White;
        }

        //Devuelve una lista de las posiciones en donde estan mis piedras
        public List<Position> stones()
        { 
            List<Position> positions= new List<Position>();
            foreach (Group aGroup in Groups) {
                foreach (Position aPotisiotn in aGroup.Positions) {
                    positions.Add(aPotisiotn);
                }
            }
            return positions;
        }

        //Devuelve una lista de las posiciones en donde estan mis piedras
        public bool hasStone(Coordinate aCoordinate)
        {
            foreach (Position aPostion in stones())
            {
                if (aCoordinate.x == aPostion.Coordinate.x && aCoordinate.y == aPostion.Coordinate.y)
                    return true;
            }
            return false;
        }

        public abstract String playerType();

        public String colourString() {
            if (isWhite())
                return "Blancas";
            else
                return "Negras";
        }

        public virtual void setMove(Position aPosition) { }

        public virtual Boolean isHumanPlayer() { return false; }


        public virtual void play()
        {
            HasMadeAFirstMove = true;
        }

//SETER GETERS
        public Player Opponent
        {
            get { return _opponent; }
            set { _opponent = value; }
        }

        public List<Group> Groups
        {
            get { return _groups; }
        }


        public Color Color
        {
            get { return _color; }
        }

        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public void removeGroup(Group aGroupToRemove)
        {
            _groups.Remove(aGroupToRemove);
        }


        public Position LastPlayed
        {
            get { return _lastPlayed; }
            set { _lastPlayed = value; }
        }

        public bool HasMadeAFirstMove
        {
            get { return _hasMadeAFirstMove; }
            set { _hasMadeAFirstMove = value; }
        }

    }
}
